// package com.syg.config;
//
// import org.springframework.cloud.gateway.config.GatewayProperties;
// import org.springframework.cloud.gateway.route.RouteDefinition;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
// import org.springframework.util.CollectionUtils;
//
// import java.util.List;
// import java.util.Set;
// import java.util.stream.Collectors;
//
//
// /**
//  * <功能描述> 从Mysql中初始化路由信息,
//  * 覆盖配置文件中的路由信息
//  *
//  * @author syg
//  * @date 2020/12/24 13:20
//  */
// @Configuration
// public class MySqlRouteConfig {
//
//     private final IRoutePropertiesService routePropertiesService;
//     private final IGatewayRouteService gatewayRouteService;
//
//     public MysqlRouteConfig(IRoutePropertiesService routePropertiesService, IGatewayRouteService gatewayRouteService) {
//         this.routePropertiesService = routePropertiesService;
//         this.gatewayRouteService = gatewayRouteService;
//     }
//
//     /**
//      * 初始化 gatewayProperties 中的 route
//      *
//      * @param gatewayProperties
//      * @return
//      */
//     @Bean
//     public List<RouteDefinition> initMysqlRouteDefinition(GatewayProperties gatewayProperties) {
//         List<RouteDefinition> gatewayPropertiesRoutes = gatewayProperties.getRoutes();
//
//         // 初始化数据库路由信息
//         List<RouteDefinition> routeDefinitionList = routePropertiesService.getRouteDefinitionList();
//         if (CollectionUtils.isEmpty(gatewayProperties.getRoutes()) && CollectionUtils.isEmpty(routeDefinitionList)) {
//             throw new BizBaseException(HprmcExceptionCode.ROUTE_NOT_FOUND);
//         }
//
//         Set<String> routeIds = routeDefinitionList.stream()
//                 .map(RouteDefinition::getId).collect(Collectors.toSet());
//         if (gatewayPropertiesRoutes.stream().anyMatch(r -> routeIds.contains(r.getId()))) {
//             throw new BizBaseException(HprmcExceptionCode.ROUTE_INIT_CONFLICT);
//         }
//
//         //将配置文件中的路由信息添加到 InMemoryRouteDefinitionRepository 成员变量中
//         if (!CollectionUtils.isEmpty(gatewayPropertiesRoutes)) {
//             gatewayPropertiesRoutes.forEach(gatewayRouteService::addInMemoryRouteRefresh);
//         }
//
//         //写到 InMemoryRouteDefinitionRepository 成员初始化缓存
//         if (!CollectionUtils.isEmpty(routeDefinitionList)) {
//             routeDefinitionList.forEach(gatewayRouteService::addInMemoryRouteRefresh);
//         }
//         return routeDefinitionList;
//     }
// }
